package com.token.controller;

import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import com.token.config.ElasticSearchClientConnect;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @auther: henriy
 * @Date: 2024/1/4
 */
@Api(tags = "ES接口类")
@RestController
@RequestMapping("/test")
public class ElasticsearchController {

    @Autowired
    private ElasticSearchClientConnect elasticSearchClientConnect;

    /**
     * 新建my_index索引
     *
     * @return
     * @throws IOException
     */
    @ApiOperation("新建my_index索引")
    @GetMapping("/createIndex")
    public Boolean createIndex() throws IOException {

        CreateIndexResponse createIndexResponse = elasticSearchClientConnect.restClient()
                .getElasticsearchClient()
                .indices()
                .create(c -> c.index("my_index"));
        // 打印结果
        System.out.println(createIndexResponse.acknowledged());
        // 关闭连接
        elasticSearchClientConnect.restClient()
                .getElasticsearchTransport()
                .close();
        elasticSearchClientConnect.restClient()
                .getRestClient()
                .close();
        return createIndexResponse.acknowledged();
    }

    /**
     * 查询索引
     *
     * @throws IOException
     */
    @ApiOperation("查询索引")
    @GetMapping("/selectIndex")
    public void selectIndex() throws IOException {
        GetIndexResponse getIndexResponse = elasticSearchClientConnect.restClient()
                .getElasticsearchClient()
                .indices()
                .get(e -> e.index("my_index"));

        // 打印结果
        System.out.println("getIndexResponse.result() = " + getIndexResponse.result());
        System.out.println("getIndexResponse.result().keySet() = " + getIndexResponse.result().keySet());

        // 关闭连接
        elasticSearchClientConnect.restClient()
                .getElasticsearchTransport()
                .close();
        elasticSearchClientConnect.restClient()
                .getRestClient()
                .close();
    }

    /**
     * 删除索引
     *
     * @return
     * @throws IOException
     */
    @ApiOperation("删除索引")
    @GetMapping("/deleteIndex")
    public Boolean deleteIndex() throws IOException {
        // 删除索引
        DeleteIndexResponse deleteIndexResponse = elasticSearchClientConnect.restClient()
                .getElasticsearchClient()
                .indices()
                .delete(e -> e.index("jing_index"));
        System.out.println("删除操作 = " + deleteIndexResponse.acknowledged());
        // 关闭连接
        elasticSearchClientConnect.restClient().getElasticsearchTransport().close();
        elasticSearchClientConnect.restClient().getRestClient().close();
        return deleteIndexResponse.acknowledged();
    }

}

